package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.scm.entity.ScmChainFina;
import org.jeecg.modules.scm.entity.ScmCloudChain;
import org.jeecg.modules.scm.service.IScmChainFinaService;
import org.jeecg.modules.scm.service.IScmCloudChainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 /**
 * @Description: scm_chain_fina
 * @Author: jeecg-boot
 * @Date:   2021-11-23
 * @Version: V1.0
 */
@Api(tags="云链单融资申请")
@RestController
@RequestMapping("/chain_fina/scmChainFina")
@Slf4j
public class ScmChainFinaController extends JeecgController<ScmChainFina, IScmChainFinaService> {
	@Autowired
	private IScmChainFinaService scmChainFinaService;
	@Autowired
	private IScmCloudChainService iScmCloudChainService;

	 @Autowired
	 @Lazy
	 private RedisUtil redisUtil;
	/**
	 * 分页列表查询
	 *
	 * @param scmChainFina
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "scm_chain_fina-分页列表查询")
	@ApiOperation(value="scm_chain_fina-分页列表查询", notes="scm_chain_fina-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ScmChainFina scmChainFina,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ScmChainFina> queryWrapper = QueryGenerator.initQueryWrapper(scmChainFina, req.getParameterMap());
		Page<ScmChainFina> page = new Page<ScmChainFina>(pageNo, pageSize);
		IPage<ScmChainFina> pageList = scmChainFinaService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param scmChainFina
	 * @return
	 */
	@AutoLog(value = "scm_chain_fina-添加")
	@ApiOperation(value="scm_chain_fina-添加", notes="scm_chain_fina-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ScmChainFina scmChainFina) {
		scmChainFinaService.save(scmChainFina);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param scmChainFina
	 * @return
	 */
	@AutoLog(value = "scm_chain_fina-编辑")
	@ApiOperation(value="scm_chain_fina-编辑", notes="scm_chain_fina-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ScmChainFina scmChainFina) {
		scmChainFinaService.updateById(scmChainFina);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "scm_chain_fina-通过id删除")
	@ApiOperation(value="scm_chain_fina-通过id删除", notes="scm_chain_fina-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		scmChainFinaService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "scm_chain_fina-批量删除")
	@ApiOperation(value="scm_chain_fina-批量删除", notes="scm_chain_fina-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.scmChainFinaService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "scm_chain_fina-通过id查询")
	@ApiOperation(value="scm_chain_fina-通过id查询", notes="scm_chain_fina-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ScmChainFina scmChainFina = scmChainFinaService.getById(id);
		if(scmChainFina==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(scmChainFina);
	}


	 /**
	  * 通过id查询
	  *
	  * @param applyNo
	  * @param chainIds
	  * @param state
	  * @return
	  */
	 @AutoLog(value = "云链单融资申请关联")
	 @ApiOperation(value="云链单融资申请关联", notes="云链单融资申请关联")
	 @GetMapping(value = "/finaChainRs")
	 public Result<?> finaChainRs(@RequestParam(name="applyNo",required=true) Integer applyNo,
								  @RequestParam(name="chainIds",required=true) String chainIds,
								  @RequestParam(name="state",defaultValue = "0") Integer state
								  ) {
		 LoginUser sysUser = SecurityUtils.getSubject().getPrincipal() != null ?
				 (LoginUser) SecurityUtils.getSubject().getPrincipal() : null;
		 if(sysUser==null){
			 throw new JeecgBootException("用户不存在");
		 }
		 Date cur = new Date();
		 String[] chains = chainIds.split(",");
		 for(String chain:chains){
		 	ScmChainFina scmChainFina = new ScmChainFina();
		 	scmChainFina.setApplyNo(applyNo);
		 	scmChainFina.setChainNo(chain);
		 	scmChainFina.setState(state);
		 	scmChainFinaService.save(scmChainFina);
		 	//融资中

			 ScmCloudChain scmCloudChain = iScmCloudChainService.lambdaQuery()
					 .eq(ScmCloudChain::getChainNo, chain)
					 .one();
			 System.out.println(scmCloudChain+"--------------");
			 redisUtil.set("cloud_chain_"+scmCloudChain.getChainNo(),scmCloudChain.getStauts());
			 scmCloudChain.setStauts(4);
			 iScmCloudChainService.updateById(scmCloudChain);
		 }
		 return Result.OK("添加成功");
	 }
	 /**
	  * 获取融资申请的所有云链单号
	  * **/
	 @AutoLog(value = "获取融资申请的所有云链单号")
	 @ApiOperation(value="获取融资申请的所有云链单号", notes="获取融资申请的所有云链单号")
	 @GetMapping(value = "/getAllchain")
	 public Result<?> getAllchain(@RequestParam(name="applyNo",required=true) Integer applyNo){
		 List<ScmChainFina> list = scmChainFinaService.lambdaQuery().eq(ScmChainFina::getApplyNo, applyNo).list();
		 List<String> ids = new ArrayList<>();
		 for(ScmChainFina scmChainFina:list){
		 	ids.add(scmChainFina.getChainNo());
		 }
		 String s = StringUtils.join(ids, ',');
		 return Result.OK(s);
	 }


 }
